package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class IndexController {
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public Object index(Model model,HttpServletRequest request, HttpServletResponse response){
        //从浏览器cookie解析出token值
        String token = CookieUtil.getToken(request, response);
        //从redis中通过token获取得到UserInfo对象
        UserInfo userInfo = userInfoRedisService.getUserInfoByToken(token);
        //共享出去
        request.getSession().setAttribute("userInfo",userInfo);
        //获取cookie信息

        //tcs
        model.addAttribute("tcs",travelCommendService.getTop5());

        //sc
        model.addAttribute("sc",strategyCommendService.getTop5().get(0));
        return "/index/index";
    }

    @RequestMapping("index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){
        //热门游记
        model.addAttribute("pageInfo",travelService.query(qo));
        return "/index/travelPageTpl";
    }


}
